<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/20
 * Time: 23:07
 */
namespace app\share\model;

class Ping
{
    /**
     * ping域名或者ip接口
     *@param  address   string    ip或者域名
     *@return  array
     */
    public function pingAddress($address)
    {
        $ping_cmd = "ping -c 1 -w 1 " . $address;
        exec($ping_cmd, $info);
        if($info == null)
        {
            $result['ip'] = $address;
            $result['tip'] = "ip错误";
            return ['code'=>401,'msg'=>"ip错误",'data'=>$result];
        }
        //判断是否丢包
        $str1 = $info['4'];
        $str2 = "1 packets transmitted, 1 received, 0% packet loss, time 0ms";
        $result['ip'] = $address;
        if(trim($str1)===$str2)
        {
            $result['tip'] = "畅通";
            return ['code'=>200,'msg'=>"畅通",'data'=>$result];
        }else
        {
            $result['tip'] = "超时";
            return ['code'=>402,'msg'=>"超时",'data'=>$result];
        }
    }
}